﻿@model  RBAC.Web.Models.DomainModel
@using RBAC.Web.CustomHelpers
@using RBAC.Web.Mapper
@using RBAC.Web.Utilities
@{
    ViewBag.Title = "Edit Domain";

			string returnUrl = Url.Action("ListDomain", "Domain");
	}
@section Styles {
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/form.js?3")" type="text/javascript"></script>
    <link href="@Url.Content("~/Content/themes/base/jquery.ui.all.css")" rel="stylesheet" />
    <link href="@Url.Content("~/Content/themes/base/jquery-ui.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/themes/base/jquery.ui.datepicker.css")" rel="stylesheet" type="text/css" />

	<link href="@Url.Content("~/Content/bootstrap/css/utility.css")" rel="stylesheet"		type="text/css">

    <script type="text/javascript">
        $(function () {
            //How to setup Datepicker
			            SetDatePicker("FreeDate");
            
			//How to setup Lookup
			
			//Master Detail
			 
							 
							 
							 
				 
					SetOneToManyFor("Function", '@Url.Action("DeleteFunctions", "Function")', '@Url.Action("EditDomain", "Domain", new { DomainName = Model.DomainName })', "form-Domain");
							        });
    </script>
}


	<div class="row">
        <div class="col-lg-12">
             <h2 class="page-header">Domain</h2>
        </div>
    </div> 

@using (Html.BeginForm("Update", "Domain", FormMethod.Post, new { @id = "form-Domain",@class = "form-horizontal", role = "form"  }))
{
    
   
	//version 1.4 Bootstrap 7/4/2557
<div class="panel panel-primary">
	<div class="panel-heading">DomainModel</div>
	<div class="panel-body">
    

							
	
      
				@Html.HiddenFor(model => model.Id)
						
		   <div class="form-group row">	   

      									 
			
												<div class="col-md-6">
                 <div class="col-md-4">
				<label class = "word-control-res">@Html.LabelFor(model => model.DomainName)</label>
				</div>
			    <div class="col-md-8">
                @Html.TextBoxFor(model => model.DomainName, new{@class="form-control"})
				
				 </div>
				 <div class="col-md-offset-4 col-md-8">
				   @Html.ValidationMessageFor(model => model.DomainName)
				</div>
				</div>
									@Html.HiddenFor(model => model.Id)
					<input type="hidden" name="Mode" value="Edit" />
					<input type="hidden" name="OldValue" value="@(Model.DomainName == null ? "" : Model.DomainName )" />
						
									 
			
												<div class="col-md-6">
                 <div class="col-md-4">
				<label class = "word-control-res">@Html.LabelFor(model => model.Description)</label>
				</div>
			    <div class="col-md-8">
                @Html.TextBoxFor(model => model.Description, new{@class="form-control"})
				
				 </div>
				 <div class="col-md-offset-4 col-md-8">
				   @Html.ValidationMessageFor(model => model.Description)
				</div>
				</div>
						
															
		</div>
		
		<div class="col-md-6">   
				<div class ="btn-group">
					<label><input type="button" name="btnBack" value="Cancel" class="btn btn-primary" id="button-back" onclick="location.href='@returnUrl'"/>   </label>
					<label><input type="Button" value="Save" id="button-save" class="btn btn-primary" onclick="JavaScript: SaveButtonClick()" />				</label>
					<label><input type="button" name="btnDelete" value="Delete" class="btn btn-primary" id="button-delete" onclick="JavaScript: DeleteButtonClick()" />  </label>
				</div>
		</div>

  


        </div>
    </div> 

// Master Detail Dest Class
								
	bool highlightFunction = false;
    if (ViewData["FunctionId"] != null)
    {
        try
        {
            int index = Model.Functions.IndexOf(Model.Functions.FirstOrDefault(m => m.Id == Convert.ToInt32(ViewData["FunctionId"])));
            if (index > -1)
            {
                var item = Model.Functions.ElementAt(index);
                Model.Functions.RemoveAt(index);
                Model.Functions.Insert(0, item);

                highlightFunction = true;
            }
        }
        catch { }
    }
	var Functiongrid = new WebGrid(source: Model.Functions, canPage: false);
	if (highlightFunction)
    {
        Functiongrid.SelectedIndex = 0;
    }
    <input type="hidden" id="Function_hid_delete" value="Delete" name="DeleteButton" /> 
   
        @Html.Hidden("Domain_Id", Model.Id)
    
 


	<div>
	@Html.DisplayMessage(Convert.ToString(ViewData["FunctionMessage"]), ViewData["FunctionStatus"] as bool?)
	</div>

        <div class="modal" id="Function-dialog-alert">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                            <h4 class="modal-title">Delete SaleOrder</h4>
                        </div>
                        <div class="modal-body">
                            No Function was selected.
                        </div>
                        <div class="modal-footer">
                            <a href="#" data-dismiss="modal" class="btn btn-primary">Ok</a>

                        </div>
                    </div>
                </div>
            </div>

  
            <div class="modal" id="Function-confirm-delete">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                            <h4 class="modal-title">Delete SaleOrder</h4>
                        </div>
                        <div class="modal-body">
                            Do you want to delete?
                        </div>
                        <div class="modal-footer">
                            <button type="button" data-dismiss="modal" id="Function-delete-data" class="btn btn-primary">YES</button>
                            <button type="button" data-dismiss="modal" class="btn btn-primary">NO</button>
                        </div>
                    </div>
                </div>
            </div>

	
		
    <div class="panel panel-primary">
	<div class="panel-heading">DomainModel</div>
	<div class="panel-body">


	 	<div class="col-md-6">   
		<div class ="btn-group">
			<label><input class="btn btn-primary" id="Function-button-add" type="button" value="New" onclick="location.href='@Url.Action("Add", "Function", new { Domain_id = Model.Id, DomainName = Model.DomainName })'"/></label>
			<label><input class="btn btn-primary" type="button" value="Delete" id="Function-button-delete" /></label>
		</div>
		</div>  


    @if (Model.Functions != null && Model.Functions.Count > 0)
    {    
        <div style="overflow:scroll; width: 100%; height:auto; max-height: 400px;">
		@Functiongrid.GetHtml( 
        htmlAttributes: new { id = "Function"},
        tableStyle:  "table table-striped table-bordered table-hover dataTable no-footer grid",
        rowStyle: "row-a",
        alternatingRowStyle: "row-b",
		selectedRowStyle: "selected-row",
        columns: Functiongrid.Columns(
            Functiongrid.Column(header: "", style: "width_0per", format: @<text><input name="FunctionCheckbox" type="checkbox" value="@item.Id" /></text>),
            Functiongrid.Column(header: "#",  style: "width_0per",format: item => item.WebGrid.Rows.IndexOf(item) + 1)
            //TODO : สามารถ Edit ได้ --
            // ทดสอบ Edit  
        					
			
			
												
							
						@* ถ้า attribute ของลูก เป็น LinkEdit *@
			,Functiongrid.Column(header: "FuncName", style: "width_0per", columnName: "FuncName",
            format: item =>
            {
				string link = "<a href=\"{0}?FuncName={2}\" class=\"word_wrap\">{1}</a>";
                return Html.Raw(string.Format(link, @Url.Content("~/Function/EditFunction"), item.Value.FuncName, item.Value.FuncName));
			})
			
			
												
							
			
			
			           @* ถ้า attribute ของลูก ไม่เป็น LinkEdit , DataType เป็น String *@
		   , Functiongrid.Column(header: "Description", canSort: false, style: "width_100per", columnName: "Description",
                format: item =>
                {
                    string Description = "<div class=\"word_wrap\">{0}</div>";
                    return Html.Raw(string.Format(Description, String.IsNullOrWhiteSpace(item.Description) ? "" : item.Value.Description));
                })   
			

			
						
							
			
			
						@* START ดึง External Lookup Field *@
			 
			

						
							
			
			
												
							
			
			
												
							
			
			
						@* START ดึง External Lookup Field *@
									@* ถ้า Lookup attribute เป็น String *@
			, Functiongrid.Column(header: "DomainLookupDomainName", canSort: false, style: "width_100per", columnName: "DomainLookupDomainName",
                format: item =>
                {
                    string DomainName = "<div class=\"word_wrap\">{0}</div>";
                    return Html.Raw(string.Format(DomainName, String.IsNullOrWhiteSpace(item.DomainLookupDomainName) ? "" : item.Value.DomainLookupDomainName));
                })  

						@* END ดึง External Lookup Field *@
			 
			

						
		             
        ))
		</div>
	}
    else
    {
            <div class="emptyRow">No Function for this Domain.</div>
    }

	</div>
	</div>

		}




	<div class="modal" id="Delete">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                            <h4 class="modal-title">Delete Domain</h4>
                        </div>
                        <div class="modal-body">
                            Do you want to delete?
                        </div>
                        <div class="modal-footer">
                            <button type="button" data-dismiss="modal" id="confirm-delete" class="btn btn-primary">YES</button>
                            <button type="button" data-dismiss="modal" class="btn btn-primary">NO</button>
                        </div>
                    </div>
                </div>
	</div>


           <div class="modal" id="Save">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                            <h4 class="modal-title">Save Domain</h4>
                        </div>
                        <div class="modal-body">
                            Do you want to save?
                        </div>
                        <div class="modal-footer">
                            <button type="button" data-dismiss="modal" id="confirm-save" class="btn btn-primary">YES</button>
                            <button type="button" data-dismiss="modal" class="btn btn-primary">NO</button>
                        </div>
                    </div>
                </div>
            </div>

<script type="text/javascript">
	$(function () {

        $("#button-delete").click(function () {
              
          
            $('#Delete').modal('show');
            $('#confirm-delete').click(function () {
                $.post('@Url.Action("DeleteDomainById", "Domain")',$("#form-Domain").serialize(),function (data) {
                    var s = '@returnUrl'
                    if(s.indexOf("?") !== -1)
                        window.location.href = s + '&result=' + data + '&operation=delete';   
                    else
                        window.location.href = s + '?result=' + data + '&operation=delete';
                });
                         
                   
            });
        });
    
 

        $("#button-save").click(function () {
            
            $('#Save').modal('show'); 
            $('#confirm-save').click(function () {
               $("#form-Domain").submit();
            });
        });
    });
</script>